package com.vito.oa.act.action;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.extend.model.OaActExApproveInfoModel;
import com.vito.oa.act.service.OaActCCService;
/**
 * 审批抄送人-控制类
 * @author renhh
 *
 */
@Controller
@RequestMapping("oa/actCC")
public class OaActCCAction extends BaseAction {
	@Reference
	private OaActCCService oaActCCService;
	private static final Logger log = LoggerFactory.getLogger(OaActCCAction.class);
	
	@ResponseBody
	@RequestMapping(value = "/queryCCUsers")
	public Result queryCCUsers(String businessId) {
		Result rs = new Result();
		try{
			if(Validata.isNullOrEmpty(businessId)){
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setData("流程编码不能为空");
				return rs;
			}
			rs.setCode(Result.CODE_OK);
			rs.setData(oaActCCService.queryCCUsers(businessId));
		}catch (Exception e) {
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}	
		return rs;
	}
	
	@ResponseBody
	@RequestMapping("/queryCCtoMePage")
	public Result queryCCtoMePage(OaActExApproveInfoModel actExApproveInfoModel){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}	
			actExApproveInfoModel.setUserid(user.getUserId());
			//默认取审批模块
			if(Validata.isNullOrEmpty(actExApproveInfoModel.getMoudelType())){
				actExApproveInfoModel.setMoudelType(OaActContants.PROCESS_MOUDEL_TYPE_APPROVAL);
			}
			if(!Validata.isNullOrEmpty(actExApproveInfoModel.getEndtime())){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				actExApproveInfoModel.setEndtime(df.format(df.parse(actExApproveInfoModel.getEndtime()))+" 23:59:59");
			}	
			rs.setData(oaActCCService.queryCCtoMePage(actExApproveInfoModel));
			rs.setCode(Result.CODE_OK);
		}catch(Exception e){
			log.error("查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
	@ResponseBody
	@RequestMapping("/saveProcessCopyPerson")
	public Result saveProcessCopyPerson(@RequestBody Map<String,Object> map){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}	
			if(!map.isEmpty() && map.containsKey("processkey") && map.containsKey("userIds")){
				if(Validata.isNullOrEmpty(map.get("processkey").toString())){
					rs.setCode(Result.CODE_PARA_ERROR);
					rs.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED,"流程key"));
					return rs;
				}
				map.put("topDeptId", user.getDeptParentId());
			}
			
			if (oaActCCService.saveProcessCopyPerson(map)) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("设置抄送人成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("设置抄送人失败");
			}
		}catch(Exception e){
			log.error("保存异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
	@ResponseBody
	@RequestMapping("/queryProcessCopyPerson")
	public Result queryProcessCopyPerson(@RequestBody Map<String,String> map){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (null == user) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}	
			if(!map.isEmpty() && map.containsKey("processkey")){
				if(Validata.isNullOrEmpty(map.get("processkey").toString())){
					rs.setCode(Result.CODE_PARA_ERROR);
					rs.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED,"流程key"));
					return rs;
				}
				map.put("topDeptId", user.getDeptParentId());
			}
			rs.setData(oaActCCService.queryProcessCopyPerson(map));
			rs.setCode(Result.CODE_OK);
			rs.setMsg("查询抄送人成功");
		}catch(Exception e){
			log.error("查询抄送人异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
	
}
